﻿<%@ WebHandler Language="C#" Class="Permissions" %>

using System;
using System.Web;
using System.Web.SessionState;
public class Permissions : IHttpHandler, IRequiresSessionState
{
    Spring.Context.IApplicationContext appContext = Spring.Context.Support.ContextRegistry.GetContext();
    GONES.Tools.Common.LoginInfo loginInfo = new GONES.Tools.Common.LoginInfo();
    public string ErrMsg { get; set; }
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        string title = context.Request["title"];
        if (context.Session["LoginInfo"] != null)
        {
            loginInfo = (GONES.Tools.Common.LoginInfo)context.Session["LoginInfo"];
        }
        if (string.IsNullOrEmpty(title))
        {
            failure("无指定操作");
        }
        else
        {
            switch (title)
            {
                case "add":
                    if (Add(context.Request["pname"], context.Request["pcode"], 
                         context.Request["iconcls"],context.Request["sortnum"],context.Request["remark"]))
                    {
                        context.Response.Write(success("", "SysPermissionList"));
                    }
                    else
                    {
                        context.Response.Write(failure("数据库连接错误"));
                    }
                    break;
                case "delete":
                    if (Del(context.Request["ids"]))
                    {
                        context.Response.Write(success("", "SysPermissionList"));
                    }
                    else
                    {
                        context.Response.Write(failure(ErrMsg));
                    }
                    break;
                case "updata":
                    if (Updata(context.Request["permissionsid"], context.Request["pname"], context.Request["pcode"],
                         context.Request["iconcls"], context.Request["sortnum"], context.Request["remark"]))
                    {
                        context.Response.Write(success("", "SysPermissionList"));
                    }
                    else
                    {
                        context.Response.Write(failure("修改失败!"));
                    }
                    break;
            }
        }
    }

    public bool Del(string id)
    {
        var navManager = appContext.GetObject("S_NavManager") as GONES.IBLL.IS_NavManager;
        try
        {
            if (navManager.GetNavCountByParentId(id) > 0)
            {
                ErrMsg = "该菜单下存在子菜单不能删除！";
                return false;
            }
            navManager.Delete(id);
            return true;
        }
        catch { return false; }
    }

    private GONES.Model.S_Permissions GetPermissionsModel(string permissionsid, string pname, string pcode, string iconcls, string sortnum, string remark)
    {
        var permissionsManager = appContext.GetObject("S_PermissionsManager") as GONES.IBLL.IS_PermissionsManager;
        GONES.Model.S_Permissions permissions = new GONES.Model.S_Permissions();
        if (permissionsid.Trim() != "")
            permissions = permissionsManager.Get(permissionsid.Trim());
        permissions.ID = permissionsid.Trim();
        permissions.Pname = pname.Trim();
        permissions.Pcode = pcode.Trim();
        permissions.Iconcls = iconcls.Trim();
        permissions.SortNum = int.Parse(sortnum.Trim());
        permissions.Remark = remark.Trim();
        return permissions;
    }
    public bool Add(string pname, string pcode, string iconcls, string sortnum, string remark)
    {
        var permissionsManager = appContext.GetObject("S_PermissionsManager") as GONES.IBLL.IS_PermissionsManager;
        GONES.Model.S_Permissions permissions = new GONES.Model.S_Permissions();
        try
        {
            permissions = GetPermissionsModel("", pname, pcode, iconcls, sortnum, remark);
            permissions.ID = GONES.Tools.Common.Rnd.Guid.ToString().Trim();
            permissions.CreateUser = loginInfo.UserName.Trim();
            permissionsManager.Add(permissions);
            return true;
        }
        catch
        {
            return false;
        }
    }

    public bool Updata(string permissionsid, string pname, string pcode, string iconcls, string sortnum, string remark)
    {
        var permissionsManager = appContext.GetObject("S_PermissionsManager") as GONES.IBLL.IS_PermissionsManager;
        GONES.Model.S_Permissions permissions = new GONES.Model.S_Permissions();
        try
        {
            permissions = GetPermissionsModel(permissionsid, pname, pcode, iconcls, sortnum, remark);
            permissions.ModifyUser = loginInfo.UserName;
            permissionsManager.Edit(permissions);
            return true;
        }
        catch
        {
            return false;
        }
    }

    public string success()
    {
        return "{\"statusCode\":\"200\", \"message\":\"操作成功!\"}";
    }
    public string success(string str, string navTabId)
    {
        return "{\"statusCode\":\"200\", \"message\":\"操作成功!" + str + "\",\"navTabId\":\"" + navTabId + "\"}";
    }
    public string failure()
    {
        return "{\"statusCode\":\"300\", \"message\":\"操作失败\"}";
    }
    public string failure(string str)
    {
        return "{\"statusCode\":\"300\", \"message\":\"操作失败!" + str + "\"}";
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}